#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/2/9 11:04
# @Author : Cdx
# @Site : 测试成功的IP在proxies.txt
# @File : A8.py
# @Software: PyCharm
import re
from concurrent.futures.thread import ThreadPoolExecutor
import requests

T_SIZE=10   #线程池的线程数量
IP_SIZE=100     #需要测试ip的数量
n=1     #n=0测试https，等于其他是测试http

'''获取ip组'''
def get_ip(i):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0",
        "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
        "Accept-Encoding": "gzip, deflate",
        "Connection": "keep-alive"
    }
    r = requests.get('http://www.89ip.cn/tqdl.html?num={}&address=&kill_address=&port=&kill_port=&isp='.format(i),
                     headers=headers)
    # result = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", r.text)
    result = re.findall(r"[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}:[0-9]{2,4}", r.text)
    return result

'''测试'''
def check(ip_prot, n):
    try:
        if(n==0):
            requests.get('http://icanhazip.com/', proxies={"https":ip_prot}, timeout=5)
        else:
            requests.get('http://www.baidu.com/', proxies={"http": ip_prot}, timeout=5)
    except:
        print('{}失败'.format(ip_prot))
    else:
        print('{}----------------------->成功'.format(ip_prot))
        with open("proxies.txt", "a", encoding="utf-8") as f:
            if(n==0):
                f.write('https:\t'+ip_prot + '\n')
            else:
                f.write('http:\t' + ip_prot + '\n')


if __name__ == '__main__':
    executor = ThreadPoolExecutor(max_workers=T_SIZE)  # 线程池数量
    ips = get_ip(IP_SIZE)
    for i in range(len(ips)):
        executor.submit(check, ips[i], n)


